package com.example.guideapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.guideapp.R;
import com.example.guideapp.bean.HistoryDay;
import com.example.guideapp.utils.AppURL;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HistoryDayActivity extends AppCompatActivity {

    private static final String key = "221a785625564";

    private ListView lv_history;
    private RelativeLayout relativeLayout;
    private ProgressBar pb_load;
    private TextView tv_load;

    private List<HistoryDay.ResultBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_day);

        lv_history = (ListView) findViewById(R.id.lv_history);
        relativeLayout = (RelativeLayout) findViewById(R.id.rl);
        pb_load = (ProgressBar) findViewById(R.id.pb_load);
        tv_load = (TextView) findViewById(R.id.tv_load);

        long time = System.currentTimeMillis();
        SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
        Date d = new Date(time);

        lv_history.setVisibility(View.INVISIBLE);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://apicloud.mob.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        AppURL appURL = retrofit.create(AppURL.class);

        Call<HistoryDay> call = null;
        try {
            call = appURL.getHsitoryDayData(URLEncoder.encode(key,"UTF-8"),URLEncoder.encode(sdf.format(d),"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }


        call.enqueue(new Callback<HistoryDay>() {
            @Override
            public void onResponse(Call<HistoryDay> call, Response<HistoryDay> response) {
                if (response != null){
                    HistoryDay history = response.body();
                    list = history.getResult();

                    lv_history.setAdapter(new MyAdapter());

                    lv_history.setVisibility(View.VISIBLE);
                    relativeLayout.setVisibility(View.GONE);
                }
            }

            @Override
            public void onFailure(Call<HistoryDay> call, Throwable t) {
                Log.e("aa", t.toString() );
            }
        });


        lv_history.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(HistoryDayActivity.this,ShowHistoryDayActivity.class);
                intent.putExtra("bean",list.get(i));
                startActivity(intent);
            }
        });
    }


    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (view == null){
                viewHolder = new ViewHolder();
                view = LayoutInflater.from(HistoryDayActivity.this).inflate(R.layout.history_item,null);
                viewHolder.tv_history_time = (TextView) view.findViewById(R.id.tv_history_time);
                viewHolder.tv_history_title = (TextView) view.findViewById(R.id.tv_history_title);
                view.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) view.getTag();
            }

            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            ParsePosition pos = new ParsePosition(0);
            Date strtodate = formatter.parse(list.get(i).getDate(), pos);
            SimpleDateFormat fm = new SimpleDateFormat("yyyy年MM月dd日", Locale.CHINA);

            viewHolder.tv_history_time.setText(fm.format(strtodate));

            viewHolder.tv_history_title.setText(list.get(i).getTitle());

            return view;
        }

         class ViewHolder{
            TextView tv_history_time;
             TextView tv_history_title;
        }

    }
}
